package FileManagement;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileDownload extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		req.setCharacterEncoding("euc-kr");
		String fileName = req.getParameter("fileName");
		String filePath = req.getRealPath("/") + "upload/";
		String fileURL = filePath + fileName;
		
		File file = new File(fileURL);
		byte b[] = new byte[(int)file.length()];
		res.setContentType("application/octet-stream;charset=8859_1;");
		res.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes(), "ISO8859_1")  + "\";");
		res.setHeader("Content-Transfer-Encoding", "binary;");
		if(file.isFile()) {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream bos = new BufferedOutputStream(res.getOutputStream());
			int i = 0;
			try {
				while((i = bis.read(b)) != -1) {
					bos.write(b, 0, i);
				}
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				if(bos != null) bos.close();
				if(bis != null) bis.close();
			}
		}
	}
}